// home/sport/sport.js
const call = require('../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModalStatus:false, //自定义弹窗
    box:{ //自定义运动
      name:'',
      time:[0,0], 
    },
    //运动
    imgUrl:`${call.host}/home/imgs/icon_`,
    sportArr:[{
      img:'kz',
      name:'快走',
    },{
      img:'pb',
      name:'跑步',
    },{
      img:'tjq',
      name:'太极拳',
    },{
      img:'tw',
      name:'跳舞',
    },{
      img:'yy',
      name:'游泳',
    },{
      img:'sb',
      name:'散步',
    },{
      img:'yj',
      name:'瑜伽',
    },{
      img:'ps',
      name:'爬山',
    },{
      img:'ymq',
      name:'羽毛球',
    },{
      img:'ts',
      name:'跳绳',
    },{
      img:'qzxc',
      name:'骑自行车',
    },{
      img:'jwhd',
      name:'家务活动',
    },{
      img:'plt',
      name:'爬楼梯',
    },{
      img:'ppq',
      name:'乒乓球',
    },{
      img:'wq',
      name:'网球',
    },{
      img:'pq',
      name:'排球',
    },{
      img:'zq',
      name:'足球',
    },{
      img:'gefq',
      name:'高尔夫球',
    },{
      img:'hhb',
      name:'滑旱冰',
    },{
      img:'hx',
      name:'滑雪',
    },{
      img:'hc',
      name:'划船',
    },{
      img:'xc',
      name:'洗车',
    },{
      img:'gj',
      name:'逛街',
    }]
  },

  /**
   * 初始化数据 
   */
  initData(){
    let sportTimeArr=[[],['分钟']];
    //运动时间
    for(let i=0;i<37;i++){
      sportTimeArr[0].push(5*i);
    }
    this.setData({
      sportTimeArr:sportTimeArr
    })
  },

  /**
   * 运动时间选择
   */
  changeSportTime(e){
    const index = e.currentTarget.dataset.index;
    this.setData({
      [`sportArr[${index}].time`]:e.detail.value
    })
  },

  /**
   * 自定义运动时间选择
   */
  changeBoxTime(e){
    this.setData({
      [`box.time`]:e.detail.value
    })
  },

  /**
   * 自定义运动输入
   */
  changeInput(e){
    this.setData({
      [`box.name`]:e.detail.value
    })
  },

  /**
   * 确定 
   */
  sure(){
    const data = this.data.sportArr;
    const timeArr = this.data.sportTimeArr;
    let obj = [];
    for(let i in data){
      if(data[i].time){
        if(data[i].time[0]>0){
          obj.push({
            img: data[i].img,
            name:data[i].name,
            time:timeArr[0][data[i].time[0]],
            index:i
          })
        }
      }
    }
    // console.log(obj)
    let pages = getCurrentPages();
    let prePage = pages[pages.length - 2];
    prePage.setData({
      sportArr:obj
    });
    wx.navigateBack({
      delta: 1,
    })
  },

  /**
   * 删除运动时间 
   */
  delTime(e){
    const index = e.currentTarget.dataset.index;
    this.setData({
      [`sportArr[${index}].time`]:null
    })
  },

  //显示规格弹窗
  showModal (e) {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(723).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  //隐藏规格弹窗
  hideModal (e) {
    const type = e.currentTarget.dataset.type;
    const box = this.data.box;
    const len = this.data.sportArr.length;
    if(type == "sure" && box.time[0]>0 && box.name.trim().length>0){
      this.setData({
        [`sportArr[${len}]`]: {
          name: box.name,
          time: box.time,
          img: 'default',
        }
      })
    }
    //清空
    this.setData({
      box:{name:'',time:[0,0]}
    })

    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initData();
    const arr = JSON.parse(options.arr);
    const timeArr = this.data.sportTimeArr;
    const sportArr = this.data.sportArr;
    let nowIndex = sportArr.length;
    console.log(arr);
    let obj = {};
    for(let i of arr){
      if(!sportArr[i.index]){
        obj = Object.assign(obj,{
          [`sportArr[${nowIndex}]`]: {
            img: i.img,
            name: i.name,
            time: [timeArr[0].indexOf(i.time),0]
          }
        })
        nowIndex++;
      }else{
        obj = Object.assign(obj,{
          [`sportArr[${i.index}].time`]: [timeArr[0].indexOf(i.time),0]
        })
      }
    }
    this.setData(obj);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

})